package poubelle;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.jsp.PageContext;

import org.kramaframework.exception.KramaUnknowJavascriptDependancyException;
import org.kramaframework.js.JsDpendency;
import org.kramaframework.servlet.TAjaxServlet;
import org.kramaframework.tag.widget.TWidget;
import org.kramaframework.util.TUtil;
import org.kramaframework.view.T;
import org.kramaframework.view.TContext;
import org.kramaframework.view.TLib;

import poubelle.css.CSSGlobalPanel;
import poubelle.css.CSSTemplate;
import poubelle.css.child.Background;
import poubelle.html.child.HTMLGlobalPanel;
import poubelle.html.child.HTMLLabel;


public class TGlobalPanels extends T {

	private CSSGlobalPanel cssPanel = new CSSGlobalPanel();
	private HTMLGlobalPanel htmlPanel = new HTMLGlobalPanel();
	private PageContext context;
	private String html = "";
	public static String name;
	private String id;
	private String htmlChild = "";
	private String cssChild = "";

	private String margin;
	private String padding;
	private String height;
	private String width;
	private Background background;
	private String style;

	private boolean drag = false;
	
	public static String jsCode = "";
	public static Vector dependancies;
	public static String css = "";

	public TGlobalPanels(String name, String id) {
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
		this.id = id;
		htmlPanel.setId("krama-" + this.id);
		dependancies = new Vector();
	}

	public void add(TWidget widget) throws IOException {

		widget.setParentName(name);
		this.htmlChild += widget.getHtml();
		this.cssChild += widget.getCss();
		jsCode += widget.getJavascript(jsCode);
		dependancies = widget.getJavascriptDependances(dependancies);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = "krama-" + name;
		htmlPanel.setName(this.name);
	}


	public void draw() {

		this.html = htmlPanel.starDraw();
		this.html += htmlChild;
		this.html += htmlPanel.endDraw();
		try {
			write(html);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		doStartTag();
	}
	
	public String simpleDraw(){
		
		this.html = getJsAjax();
		this.html +=getHtmlAjax();
		this.html += getCssAjax();
		return html;
	}

	
	public void setParentName(String parentName) {
		// TODO Auto-generated method stub
	}

	
	public String getHtml() {
		// TODO Auto-generated method stub
		return html;
	}

	
	public String getCSS() {
		// TODO Auto-generated method stub
		if (name != null) {
			return cssPanel.getCSS("." + name);
		} else
			return "";
	}

	private boolean visible = false;

	public void setVisible(boolean visible) {

		this.visible = visible;

		if (visible) {
			draw();
			TLib.css = getCSS() + cssChild;
			TLib.jsCode = jsCode;
			TLib.dependancies = dependancies;
		}
	}

	public String getAjax() {
		return simpleDraw();
	}
	
	public void setMargin(String margin) {
		this.margin = margin;
		cssPanel.setMargin(margin);
	}

	public void setPadding(String padding) {
		this.padding = padding;
		cssPanel.setPadding(padding);
	}

	public void setHeight(String height) {
		this.height = height;
		cssPanel.setHeight(height);
	}

	public void setWidth(String width) {
		this.width = width;
		cssPanel.setWidth(width);
	}

	public void setMargin(int margin) {
		this.margin = String.valueOf(margin) + "px";
		cssPanel.setMargin(this.margin);
	}

	public void setPadding(int padding) {
		this.padding = String.valueOf(padding) + "px";
		cssPanel.setPadding(this.padding);
	}

	public void setHeight(int height) {
		this.height = String.valueOf(height) + "px";
		cssPanel.setHeight(this.height);
	}

	public void setWidth(int width) {
		this.width = String.valueOf(width) + "px";
		cssPanel.setWidth(this.width);
	}

	public void setBackground(Background background) {
		this.background = background;
		cssPanel.setBackground(background.getBackground());
	}

	public void setStyle(String style) {
		this.style = style;
		cssPanel.setStyle(style);
	}

	public String getMargin() {
		return margin;
	}

	public String getPadding() {
		return padding;
	}

	public String getHeight() {
		return height;
	}

	public String getWidth() {
		return width;
	}

	public Background getBackground() {
		return background;
	}

	public String getStyle() {
		return style;
	}

	public void setDrag(boolean drag) {

		this.drag = drag;
		htmlPanel.setDrag(true);

		cssPanel.setStyle((style != null) ? style : "" + " position:relative;");
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		if (id != null) {
			this.id = id;
			htmlPanel.setId("krama-" + this.id);
		}
	}
	public String getHtmlAjax(){
		
		String html = htmlPanel.starDraw();
			   html += htmlChild;
			   html += htmlPanel.endDraw();
       return html;
	}
	public String getCssAjax(){
		return "<style type=\"text/css\">\n" +
				 getCSS() + cssChild +
				"</style>";
	}
	
	public String getJsAjax(){
		
		String jsSrc = "";
		String html = "";
		String jsPath;
		if(TAjaxServlet.getJsPath() == null){
			jsPath = "js/";
		}else
			jsPath = TAjaxServlet.getJsPath();
		
		
		for(Object dependancy : dependancies){
			JsDpendency jsDependency = TAjaxServlet.dependency.getDependencyByName((String)dependancy);
				
					if(jsDependency !=null){		
						html += "<script type=\"text/javascript\" " +
								"src=\""+jsPath+jsDependency.getFile()+".js\"></script>";
					}else{
						
					}
		}
		return html;
	}
}
